package com.axend.aerosense.pay.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.axend.aerosense.pay.entity.PayPurchaseRecordBean;
import com.axend.aerosense.pay.entity.PayRecordBean;
import com.axend.aerosense.pay.ui.PayHomeActivity;
import com.axend.aerosense.pay.ui.adapter.PayPurchaseRecordAdapter;
import com.axend.aerosense.pay.viewmodel.PurchaseHistroyViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.pay.R;
import com.axend.aerosense.pay.databinding.PayPurchaseRecordFragmentBinding;

import java.util.ArrayList;

public class PayPurchaseRecordFragment extends MvvmBaseFragment<PayPurchaseRecordFragmentBinding, PurchaseHistroyViewModel>
        implements ILoadDataResult<PayPurchaseRecordBean> {
    private PayPurchaseRecordAdapter payPurchaseRecordAdapter;
    private ArrayList<PayRecordBean> recordLists = new ArrayList<>();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setLoadSir(viewDataBinding.payContent);
        intView();
    }

    public void initTitle() {
        ((ITitle) getActivity()).setTitle(getString(R.string.pay_vip_purchase_record));
        ((PayHomeActivity) getActivity()).setTitleColor(Color.rgb(117, 117, 117));
        ((PayHomeActivity) getActivity()).setTitleBackground(Color.WHITE);
        ((PayHomeActivity) getActivity()).getToolBar().setNavigationIcon(R.drawable.pay_ic_back_black);
    }

    public void intView() {
        initTitle();
        payPurchaseRecordAdapter = new PayPurchaseRecordAdapter(recordLists);
        viewDataBinding.payBuyMessageRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        viewDataBinding.payBuyMessageRecyclerView.setAdapter(payPurchaseRecordAdapter);
        showLoading();
        viewModel.onLoad();

    }


    @Override
    public void onResume() {
        super.onResume();
        ImmersionBar.with(getActivity())
                //#383838
                .statusBarColor(android.R.color.white)
                .navigationBarColor(android.R.color.transparent)
                .fitsSystemWindows(true)
//                            .titleBarMarginTop(R.id.topView)
                .autoDarkModeEnable(true)
                .init();
        initTitle();
    }

    public static PayPurchaseRecordFragment getInstance() {
        return new PayPurchaseRecordFragment();
    }


    @Override
    public int getLayoutId() {
        return R.layout.pay_purchase_record_fragment;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected PurchaseHistroyViewModel getViewModel() {
        BaseViewModelFactroy<PayPurchaseRecordBean> myViewModelFactory = new BaseViewModelFactroy<PayPurchaseRecordBean>(this, this);
        return new ViewModelProvider(this, myViewModelFactory).get(PurchaseHistroyViewModel.class);
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(PayPurchaseRecordBean payPurchaseRecordBean) {
        recordLists.clear();
        recordLists.addAll(payPurchaseRecordBean.getOrders());
        payPurchaseRecordAdapter.setRecordLists(recordLists);
        if(recordLists.size() == 0){
            showEmpty();
        }
    }

    @Override
    public void loadDataFail(String decs) {

    }
}
